package com.yangrd.airship.generator.support.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yangrd
 * @date 2021/11/9
 */
public final class ObjMapper {

    public static <T, R> List<R> mapList(T[] dataArray, Function<T, R> mapper) {
        return mapList(Arrays.asList(dataArray),mapper);
    }

    public static <T, R> List<R> mapList(Collection<T> dataSet, Function<T, R> mapper) {
        return dataSet.stream().map(mapper).filter(Objects::nonNull).collect(Collectors.toList());
    }

    public static <T, R> List<R> flatMapList(Collection<T> dataSet, Function<T, Collection<R>> mapper) {
        return dataSet.stream().map(mapper).flatMap(Collection::stream).filter(Objects::nonNull).collect(Collectors.toList());
    }
}
